<template>
	<view>
		<uni-popup ref="alertDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="content" @confirm="dialogConfirm" @close="dialogClose">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getToken
	} from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				trojanCode: '',
				content: '确定拆托吗？',
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
			}
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			//that.unregisterScan();
			uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
			return true;
		},

		onShow() {
			this.orgId = uni.getStorageSync('orgId');
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},

		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						var result = common.scanCode(codeStr);
						if (result[0] == "T") {
							_this.trojanCode = result[1];
							_this.checkFinished(codeStr);
						} else {
							uni.showToast({
								title: '请正确扫描托码',
								icon: 'none',
								duration: 3000
							});
						}
					}
				});
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			//查询此托是否已完成
			checkFinished(codeStr) {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				uni.request({
					url: this.baseUrl + '/system/unpline/pallet',
					data: {
						orgId: that.orgId,
						userName: that.userName,
						cipherCode: that.trojanCode,
						type: 'remove'
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							that.content = '确定将' + codeStr + '进行拆托处理吗？';
							that.$refs.alertDialog.open();
						} else if (res.data.code == '201' || res.data.code == 201) {
							uni.showModal({
								title: '提示',
								content: '此' + codeStr + '托未完成，不允许拆托，请在组托任务中处理。',
								success: function(res) {
									if (res.confirm) {
										return;
									} else {
										return;
									}
								}
							})
						} else if (res.data.code == '203' || res.data.code == 203) {
							uni.showModal({
								title: '提示',
								content: '此' + codeStr + '托未校验，不允许拆托，请在组托任务中处理。',
								success: function(res) {
									if (res.confirm) {
										return;
									} else {
										return;
									}
								}
							})
						} else if (res.data.code == '50000') { //20240205判断组托计划锁定
							uni.showToast({
								duration: 4000,
								icon: 'none',
								title: '该组托计划正在做变更已被锁定,不允许操作!'
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			dialogConfirm() {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				uni.request({
					url: this.baseUrl + '/system/wmsbacklog/cx/rem/add',
					data: {
						orgId: that.orgId,
						userName: that.userName,
						pkOrder: that.trojanCode,
						category: 'WmUnpanelLine',
						startTime: '',
						endTime: '',
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '申请成功，等待组长处理',
								icon: 'none',
								duration: 3000
							});
							//that.unregisterScan();
							setTimeout(function() {
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			dialogClose() {
				this.$refs.alertDialog.close();
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>